# coding=utf-8
from rest_framework import serializers, exceptions
from plantform.models import *
from report.models import Report
from utils import logger


class BaseSearilizers(serializers.BaseSerializer):
    pass


class DataSourceListSerializers(serializers.ModelSerializer):
    class Meta:
        model = DataSource
        fields = '__all__'


class DataSourceCreateSerializers(serializers.ModelSerializer):
    data_source_type = serializers.CharField(required=True, max_length=200)

    class Meta:
        model = DataSource
        fields = (
            "data_source_type",
            "source_name",
            "db_type",
            "db_uri",
            "db_name",
            "db_form",
            "db_host",
            "db_port",
            "hdfs",
            "txt_file_type",
            "txt_file_path",
            "id"
        )

    def create(self, validated_data):
        logger.debug("开始创建data_source")
        data_source = DataSource(**validated_data)
        # TODO 将根据data_source_type 来确定哪些参数是必须的
        data_source.save()
        return data_source


class DataSourceUpdateSerializers(serializers.ModelSerializer):
    hdfs = serializers.CharField(required=False, max_length=200)
    txt_file_path = serializers.CharField(required=False, max_length=200)

    class Meta:
        model = DataSource
        fields = (
            "db_type",
            "db_uri",
            "db_name",
            "db_form",
            "db_port",
            "db_host",
            "hdfs",
            "txt_file_path",
            "data_source_status"
        )

    def update(self, instance, validated_data):
        logger.debug("修改data_source内容")
        instance = super(DataSourceUpdateSerializers, self).update(instance, validated_data)
        # TODO 将根据data_source_type 来确定哪些参数是必须的
        return instance


class BasePlantFormSerializers(serializers.Serializer):
    pass


# 输出源相关
class OutDataSourceListSerializers(serializers.ModelSerializer):
    class Meta:
        model = OutDataSource
        fields = '__all__'


class OutDataSourceCreateSerializers(serializers.ModelSerializer):
    class Meta:
        model = OutDataSource
        exclude = ("user", "output_source_status",)

    def create(self, validated_data):
        data_source = OutDataSource(**validated_data)
        # TODO 将根据data_source_type 来确定哪些参数是必须的
        data_source.save()
        return data_source


class OutDataSourceUpdateSerializers(serializers.ModelSerializer):
    class Meta:
        model = OutDataSource
        exclude = ("id", "user", "output_type",)

    def update(self, instance, validated_data):
        logger.debug("修改data_source内容")
        instance = super(OutDataSourceUpdateSerializers, self).update(instance, validated_data)
        # TODO 将根据data_source_type 来确定哪些参数是必须的
        return instance


# 数据配置相关
class PlantformConfigListSerializers(serializers.ModelSerializer):
    class Meta:
        model = PlantformConfig
        exclude = ("user",)


class PlantformConfigCreateSerializers(serializers.ModelSerializer):
    class Meta:
        model = PlantformConfig
        exclude = ("user",)

    def create(self, validated_data):
        logger.debug("开始创建data_source")
        data_source = PlantformConfig(**validated_data)
        # TODO 将根据data_source_type 来确定哪些参数是必须的
        data_source.save()
        return data_source


class PlantformConfigUpdateSerializers(serializers.ModelSerializer):
    class Meta:
        model = PlantformConfig
        exclude = ("user",)

    def update(self, instance, validated_data):
        logger.debug("修改data_source内容")
        instance = super(PlantformConfigUpdateSerializers, self).update(instance, validated_data)
        # TODO 将根据data_source_type 来确定哪些参数是必须的
        return instance


class TaskListSerializers(serializers.ModelSerializer):
    data_source = DataSourceListSerializers(read_only=True)
    output_data_source = OutDataSourceListSerializers(read_only=True)
    ml_config = PlantformConfigListSerializers(read_only=True)

    class Meta:
        model = Task
        exclude = ("user",)


class TaskReportSerializers(serializers.ModelSerializer):
    class Meta:
        model = TaskReport
        fields = '__all__'


class TaskCreateSerializers(serializers.ModelSerializer):
    class Meta:
        model = Task
        exclude = ("user", "task_status", "task_results", "error_info", "celery_id")

    def create(self, validated_data):
        logger.debug("开始创建data_source")
        data_source = Task(**validated_data)
        if data_source.task_name == "" or data_source.task_name == "task_name":
            data_source.task_name = data_source.data_source.data_source_type + '-' + data_source.ml_config.ml_type + '-' + data_source.output_data_source.output_type
        # 开始执行任务分析，分几步进行，第一步是保存本次执行任务的操作
        if data_source.task_description == "":
            data_source.task_description = data_source.data_source.data_source_type + '-' + data_source.ml_config.ml_type + '-' + data_source.output_data_source.output_type
        data_source.save()
        # 开始创建report
        if data_source.report_keyword:
            report = Report()
            report.task = data_source
            report.keywords = data_source.report_keyword
            report.save()
        return data_source


class TaskUpdateSerializers(serializers.ModelSerializer):
    class Meta:
        model = Task
        exclude = ("user", "id",)

    def update(self, instance, validated_data):
        logger.debug("修改data_source内容")
        instance = super(TaskUpdateSerializers, self).update(instance, validated_data)
        # TODO 将根据data_source_type 来确定哪些参数是必须的
        return instance


class TaskStartSerializers(serializers.Serializer):
    task_id = serializers.IntegerField(required=True)


class DocIndexSerializers(serializers.Serializer):
    doc_type = serializers.StringRelatedField(required=False, default='edu')
    keywords = serializers.StringRelatedField(required=False, default="")
    page = serializers.IntegerField(required=False, default=1)
